package com.voicenews.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.SequenceInputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.process.DocumentPreprocessor;

@Path("/ttsservices")
public class TTSService {
	@POST
	@Path("/audio")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getAudio(String data) {
		SequenceInputStream tmp = null;
		try {
			JSONObject json = new JSONObject(data);
			Reader reader = new StringReader(json.getString("data"));
			DocumentPreprocessor dp = new DocumentPreprocessor(reader);

			List<String> sentenceList = new LinkedList<String>();
			Iterator<List<HasWord>> it = dp.iterator();
			while (it.hasNext()) {
				StringBuilder sentenceSb = new StringBuilder();
				List<HasWord> sentence = it.next();
				for (HasWord token : sentence) {
					if (sentenceSb.length() > 1) {
						sentenceSb.append(" ");
					}
					sentenceSb.append(token);
				}
				sentenceList.add(sentenceSb.toString());
			}

			File f = new File(String.valueOf(data.hashCode()) + ".mp3");
			System.out.println(f.getAbsolutePath());
			System.out.println(System.getProperty("catalina.base"));
			OutputStream out = new FileOutputStream(f);
			byte buf[] = new byte[1024];
			int len;
			InputStream sound = null;
			List<InputStream> list = new ArrayList<InputStream>();
			Enumeration<InputStream> streamList;
			System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");
			for (String sentence : sentenceList) {
				sentence = sentence.replace("-LRB-", " ");
				URL urlTTS = new URL(
				// "http://localhost/TTSService/Service/service.svc/TTS?voiceIndex=1&rate=2&extension=mp3&text="
						"http://translate.google.com.vn/translate_tts?ie=UTF-8&q=" 
						+ sentence.replace(" ", "%20")
						+ "&tl=vi");
				System.out.println(urlTTS.toString());
				HttpURLConnection urlConn = (HttpURLConnection) urlTTS
						.openConnection();
				urlConn.addRequestProperty("User-Agent",
						"Mozilla/9.0 (compatible; MSIE 6.0; Windows NT 5.0)");
				sound = urlConn.getInputStream();
				list.add(sound);
			}
			streamList = Collections.enumeration(list);
			tmp = new SequenceInputStream(streamList);
			// while ((len = tmp.read(buf)) > 0)
			// out.write(buf, 0, len);
			out.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return Response.ok(tmp, MediaType.APPLICATION_OCTET_STREAM).build();
	}

}
